package com.xmy.cultivate.service.impl;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.xmy.cultivate.entity.*;
import com.xmy.cultivate.entity.views.ReNewDetailForTeacher;
import com.xmy.cultivate.mapper.*;
import com.xmy.cultivate.service.IQuarterService;
import com.xmy.cultivate.service.IRenewDataService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xmy.cultivate.service.IStudentTypeService;
import com.xmy.cultivate.util.ConfigData;
import com.xmy.cultivate.util.DateUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author min
 * @since 2022-11-29
 */
@Service
public class RenewDataServiceImpl extends ServiceImpl<RenewDataMapper, RenewData> implements IRenewDataService {

    @Autowired
    @Lazy
    StaffMapper staffMapper;

    @Autowired
    @Lazy
    IQuarterService iQuarterService;

    @Autowired
    @Lazy
    RenewDataMapper renewDataMapper;

    @Autowired
    @Lazy
    IStudentTypeService iStudentTypeService;

    @Autowired
    @Lazy
    OrderDetailMapper orderDetailMapper;

    @Autowired
    @Lazy
    QuarterMapper quarterMapper;

    @Autowired
    @Lazy
    CourseMapper courseMapper;

    @Autowired
    @Lazy
    GradeMapper gradeMapper;

    @Autowired
    @Lazy
    StudentGradeMapper studentGradeMapper;

    @Autowired
    @Lazy
    StudentMapper studentMapper;


    public boolean addData(Long school_id, Long student_id,Integer yearPart,Course course, Grade grade){

        RenewData renewData = new RenewData();
        QueryWrapper<RenewData> renewDataQueryWrapper = new QueryWrapper<>();
        renewDataQueryWrapper.eq("school_id",school_id);
        renewDataQueryWrapper.eq("subjects_id",course.getSubjectsId());
        renewDataQueryWrapper.eq("student_id",student_id);
        renewDataQueryWrapper.eq("year_part",yearPart);
        renewDataQueryWrapper.eq("quarter_num",course.getQuarterNum());
        renewDataQueryWrapper.eq("type",1);
        renewDataQueryWrapper.last("limit 1");

        Integer yearAndQuarter = Integer.parseInt(yearPart +"" +course.getQuarterNum());

        //查看是否已经添加过
        RenewData renewDataIsAt = renewDataMapper.selectOne(renewDataQueryWrapper);

        if(renewDataIsAt == null){
            if(grade!=null){
                Staff staff = staffMapper.findOneById(grade.getTeacherId());
                if(staff != null){
                    Integer residueCourse =  orderDetailMapper.getResidueCourseCount(school_id,student_id,course.getSubjectsId(),yearAndQuarter,course.getLessonType());
                    if(residueCourse>2){
                        renewData.setTeacherId(staff.getId());
                        renewData.setTeacherName(staff.getName());
                    }
                }
                renewData.setYearClassId(grade.getYearClassId().getKey());
            }
            renewData.setSchoolId(school_id);
            renewData.setSubjectsId(course.getSubjectsId());
            renewData.setStudentId(student_id);

            //renewData.setIsRestor();//是否校长
            Integer year_part = iQuarterService.getQuarterYearPartForNum(course.getQuarterNum());

            renewData.setYearPart(year_part);//当前报读的年份
            renewData.setQuarterNum(course.getQuarterNum());//当前报读的季度

            //下一个年份与季度
            Map<String,Integer> mapQuarter = ConfigData.getNextQuarter(year_part,course.getQuarterNum());

            renewData.setYearPartRenew(mapQuarter.get("yearPart"));//续班年份
            renewData.setQuarterNumRenew(mapQuarter.get("num"));//续班季度
            renewData.setIsRenew(0);
            renewData.setType(1);
            renewDataMapper.insert(renewData);

            renewData.setId(null);
            Map<String,Integer> mapQuarterNext = ConfigData.getNextQuarter(mapQuarter.get("yearPart"),mapQuarter.get("num"));
            renewData.setYearPartRenew(mapQuarterNext.get("yearPart"));//续班年份
            renewData.setQuarterNumRenew(mapQuarterNext.get("num"));//续班季度
            renewData.setType(2);
            renewDataMapper.insert(renewData);
        }
        return true;
    }


    /**
     * 续班类型：1报名，续费，2转课，3转班，4转校
     * @param school_id
     * @param student_id
     * @param course
     * @param grade
     * @param courseHouse
     * @param reNewType
     * @return
     */
    public boolean updateData(Long school_id, Long student_id,Course course, Grade grade,Integer courseHouse,Integer reNewType,Long orderDetailId){

        Integer year_part = iQuarterService.getQuarterYearPartForNum(course.getQuarterNum());//获取报读的年份
        QueryWrapper<RenewData> renewDataQueryWrapper = new QueryWrapper<>();
        renewDataQueryWrapper.eq("school_id",school_id);
        renewDataQueryWrapper.eq("subjects_id",course.getSubjectsId());
        renewDataQueryWrapper.eq("student_id",student_id);
        renewDataQueryWrapper.eq("year_part_renew",year_part);
        renewDataQueryWrapper.eq("quarter_num_renew",course.getQuarterNum());
        renewDataQueryWrapper.eq("type",1);
        renewDataQueryWrapper.last("limit 1");
        RenewData renewData = renewDataMapper.selectOne(renewDataQueryWrapper);

        Integer yearAndQuarter = Integer.parseInt(year_part +"" +course.getQuarterNum());


        Integer addCourseHosue = 0;
        if(courseHouse != 0){
            if(grade!=null){
                addCourseHosue = (Integer) courseHouse / grade.getStuDeductHour();//计算出课次
            }else {
                addCourseHosue = (Integer) courseHouse / 3;//计算出课次
            }

        }

        //为空表示没有需要续班的信息
        if(renewData != null){
            //还没有续班成功
            if(renewData.getIsRenew()!=1){
                //报名/续费
                if(reNewType == 1 || reNewType == 2){
                    boolean isNewStudent = iStudentTypeService.isNew(school_id,student_id,course.getSubjectsId());
                    if(isNewStudent){
                        //剩余课次
                        Integer residueCourseCount = orderDetailMapper.getResidueCourseCount(school_id,student_id,course.getSubjectsId(),yearAndQuarter,course.getLessonType());
                        addCourseHosue = addCourseHosue + residueCourseCount;
                        if(addCourseHosue >=3){
                            renewData.setIsRenew(1);//设置续班成功
                            renewData.setRenewDate(LocalDate.now());
                            renewData.setOrderDetailId(orderDetailId);
                            renewData.setRenewType(1);//续班类型1缴费，2转课
                            renewData.setIsNewStudent(1);//是否新生，1是，0否
                            renewDataMapper.updateById(renewData);
                        }
                        //设为老生
                        iStudentTypeService.seType(school_id,student_id,course.getSubjectsId(),2);
                    }else{
                        //老生
                        Integer residueCourseCount = orderDetailMapper.getResidueCourseCount(school_id,student_id,course.getSubjectsId(),yearAndQuarter,course.getLessonType());
                        addCourseHosue = addCourseHosue + residueCourseCount;
                        if(addCourseHosue >=15){
                            renewData.setIsRenew(1);//设置续班成功
                            renewData.setRenewDate(LocalDate.now());
                            renewData.setOrderDetailId(orderDetailId);
                            renewData.setRenewType(1);//续班类型1缴费，2转课
                            renewData.setIsNewStudent(0);//是否新生，1是，0否
                            renewDataMapper.updateById(renewData);
                        }
                    }
                }
            }
        }else{
            QueryWrapper<RenewData> renewDataQueryWrapperNext = new QueryWrapper<>();
            renewDataQueryWrapperNext.eq("school_id",school_id);
            renewDataQueryWrapperNext.eq("subjects_id",course.getSubjectsId());
            renewDataQueryWrapperNext.eq("student_id",student_id);
            renewDataQueryWrapperNext.eq("year_part_renew",year_part);
            renewDataQueryWrapperNext.eq("quarter_num_renew",course.getQuarterNum());
            renewDataQueryWrapperNext.eq("type",2);

            RenewData renewDataNext = renewDataMapper.selectOne(renewDataQueryWrapperNext);

            //存在隔季续班信息
            if(renewDataNext!=null){
                if(renewDataNext.getIsRenew() != 1){
                    //报名/续费
                    if(reNewType.equals(1) || reNewType.equals(2)){
                        QueryWrapper<Quarter> queryWrapper = new QueryWrapper<>();
                        queryWrapper.eq("year_part",year_part);
                        queryWrapper.eq("num",course.getQuarterNum());

                        //获取当前时间所属的季度
                        Quarter newQuarter = quarterMapper.selectOne(queryWrapper);
                        //第二次课的时间是否大于或等于当前时间
                        LocalDate nowDate = DateUtil.getNowDate();
                        if(nowDate.isBefore(newQuarter.getTwoCourseDate()) || nowDate.isEqual(newQuarter.getTwoCourseDate())){
                            if(reNewType == 1){
                                //报名
                                if(addCourseHosue >=15){
                                    //第二次课之前缴费的，计入隔季续班的人数,
                                    renewDataNext.setIsRenew(1);//计入隔季度的人数中
                                    renewDataNext.setRenewDate(LocalDate.now());
                                    renewDataMapper.updateById(renewDataNext);

                                    renewDataNext.setOrderDetailId(orderDetailId);
                                    renewDataNext.setRenewType(1);//续班类型1缴费，2转课
                                    renewDataNext.setIsNewStudent(1);//是否新生，1是，0否
                                    //设为老生
                                    iStudentTypeService.seType(school_id,student_id,course.getSubjectsId(),2);
                                }
                            }else if(reNewType == 2){
                                //转课
                                if(addCourseHosue >=2){
                                    //第二次课之前缴费的，计入隔季续班的人数,
                                    renewDataNext.setIsRenew(1);//计入隔季度的人数中
                                    renewDataNext.setRenewDate(LocalDate.now());

                                    renewDataNext.setOrderDetailId(orderDetailId);
                                    renewDataNext.setRenewType(1);//续班类型1缴费，2转课
                                    renewDataNext.setIsNewStudent(0);//是否新生，1是，0否

                                    renewDataMapper.updateById(renewDataNext);
                                    //设为老生
                                    iStudentTypeService.seType(school_id,student_id,course.getSubjectsId(),2);
                                }
                            }

                        }else {
                            //第二次课之后缴费的，重置为新生
                            //设为新生
                            iStudentTypeService.seType(school_id,student_id,course.getSubjectsId(),1);
                        }
                    }else {
                        //System.out.print("111111============");
                    }
                }
            }else {
                iStudentTypeService.seType(school_id,student_id,course.getSubjectsId(),1);
            }
        }

        return true;
    }


    /**
     * 分班更新老师
     * @param schoolId
     * @param studentId
     * @param course
     * @param grade
     * @return
     */
    public boolean updateTeacher(Long schoolId, Long studentId,Course course, Grade grade){
        RenewData renewData = this.getRenewDataForSubjects(schoolId,course.getSubjectsId(),studentId,grade.getYearPart(),course.getQuarterNum(),1);

        //System.out.print("renewData===");
        //System.out.print(renewData);

        //为空表示没有需要续班的信息
        if(renewData != null){
            Staff staff = staffMapper.findOneById(grade.getTeacherId());
            if((renewData.getTeacherId() == null || renewData.getTeacherId() ==0)){
                renewData.setTeacherId(grade.getTeacherId());
                renewData.setTeacherName(staff.getName());
                renewData.setYearClassId(grade.getYearClassId().getKey());
                renewDataMapper.updateById(renewData);
            }else if(renewData.getTeacherId().equals(grade.getTeacherId())==false){

                StudentGrade studentGrade = studentGradeMapper.getLastForStudent(schoolId,studentId,course.getSubjectsId(),grade.getYearPart(),course.getQuarterNum());
                if(studentGrade.getStudentId().equals(grade.getTeacherId())==false){
                    if(studentGrade.getUseCourseCount().equals(0)){
                        renewData.setTeacherId(grade.getTeacherId());
                        renewData.setTeacherName(staff.getName());
                        renewData.setYearClassId(grade.getYearClassId().getKey());
                    }else {
                        Integer yearAndQuarter = Integer.parseInt(grade.getYearPart() +"" +course.getQuarterNum());

                        Integer residueCount = orderDetailMapper.getResidueCourseCount(schoolId,studentId,course.getSubjectsId(),yearAndQuarter,course.getLessonType());
                        if(residueCount>2){
                            renewData.setTeacherId(grade.getTeacherId());
                            renewData.setTeacherName(staff.getName());
                            renewData.setYearClassId(grade.getYearClassId().getKey());
                        }
                    }
                }
            }
            RenewData renewData02 = this.getRenewDataForSubjects(schoolId,course.getSubjectsId(),studentId,grade.getYearPart(),course.getQuarterNum(),2);
            if((renewData02.getTeacherId() == null || renewData02.getTeacherId() ==0)){
                renewData02.setTeacherId(grade.getTeacherId());
                renewData02.setTeacherName(staff.getName());
                renewData02.setYearClassId(grade.getYearClassId().getKey());
                renewDataMapper.updateById(renewData02);
            }else if(renewData02.getTeacherId().equals(grade.getTeacherId())== false){

            }
        }
        return true;
    }





    /**
     * 转校更新续班信息
     * @param studentId 学生id
     * @param outSchoolId 转出学校id
     * @param outYearPart 转出年份
     * @param inSchoolId 转入学校id
     * @param outCourseId 转出课程id
     * @param inCourseId 转入课程id
     * @param inGradeId 转入班级id
     * @param inCourseHosue 转入课程课时
     * @param orderDetailId 订单id
     * @return
     */
    public boolean transferSchoolUpdate(Long studentId,Long outSchoolId,Integer outYearPart,Long inSchoolId,Long outCourseId,Long inCourseId, Long inGradeId,Integer inCourseHosue,Long orderDetailId){

        Course outCourse = courseMapper.selectById(outCourseId);
        Course inCourse = courseMapper.selectById(inCourseId);
        Grade inGrade = gradeMapper.selectById(inGradeId);
        //System.out.println("inCourseId:"+inCourseId);
        //System.out.println("outCourseId:"+outCourseId);

        Integer yearAndQuarter = Integer.parseInt(outYearPart +""+outCourse.getQuarterNum());
        /**
         * 剩余课次
         */
        Integer residueCourseCount =  orderDetailMapper.getResidueCourseCount(outSchoolId,studentId,outCourse.getSubjectsId(),yearAndQuarter,outCourse.getLessonType());
        boolean isNewStudent = iStudentTypeService.isNew(outSchoolId,studentId,outCourse.getSubjectsId());
        RenewData renewData = this.getRenewDataForSubjects(outSchoolId,outCourse.getSubjectsId(),studentId,outYearPart,outCourse.getQuarterNum(),1);
        RenewData renewData2 = this.getRenewDataForSubjects(outSchoolId,outCourse.getSubjectsId(),studentId,outYearPart,outCourse.getQuarterNum(),2);

        /**
         * 属于同一季度
         */

        if(inCourse.getQuarterNum() == outCourse.getQuarterNum()){
            if(isNewStudent){
                if(residueCourseCount>=7){
                    if(renewData!=null){
                        //renewData.setIsRenew(1);
                        if(inGrade!=null){
                            renewData.setSchoolId(inGrade.getSchoolId());
                            Staff staff = staffMapper.findOneById(inGrade.getTeacherId());
                            renewData.setTeacherId(inGrade.getTeacherId());
                            renewData.setTeacherName(staff.getName());
                        }else {
                            renewData.setSchoolId(inSchoolId);
                            renewData.setTeacherId(0L);
                            renewData.setTeacherName("");
                            renewData.setIsRestor(0);
                        }
                        renewDataMapper.updateById(renewData);
                    }
                    if(renewData2!=null){
                        if(inGrade!=null){
                            renewData2.setSchoolId(inGrade.getSchoolId());
                            Staff staff = staffMapper.findOneById(inGrade.getTeacherId());
                            renewData2.setTeacherId(inGrade.getTeacherId());
                            renewData2.setTeacherName(staff.getName());
                        }else {
                            renewData2.setSchoolId(inSchoolId);
                            renewData2.setTeacherId(0L);
                            renewData2.setTeacherName("");
                            renewData2.setIsRestor(0);
                        }
                        renewDataMapper.updateById(renewData2);
                    }
                }
            }else {
                if(residueCourseCount>=7){
                    if(renewData!=null){
                        if(inGrade!=null){
                            renewData.setSchoolId(inGrade.getSchoolId());
                            Staff staff = staffMapper.findOneById(inGrade.getTeacherId());
                            renewData.setTeacherId(inGrade.getTeacherId());
                            renewData.setTeacherName(staff.getName());
                        }else {
                            renewData.setSchoolId(inSchoolId);
                            renewData.setTeacherId(0L);
                            renewData.setTeacherName("");
                            renewData.setIsRestor(0);
                        }
                        renewDataMapper.updateById(renewData);
                    }
                    if(renewData2!=null){
                        if(inGrade!=null){
                            renewData2.setSchoolId(inGrade.getSchoolId());
                            Staff staff = staffMapper.findOneById(inGrade.getTeacherId());
                            renewData2.setTeacherId(inGrade.getTeacherId());
                            renewData2.setTeacherName(staff.getName());
                        }else {
                            renewData2.setSchoolId(inSchoolId);
                            renewData2.setTeacherId(0L);
                            renewData2.setTeacherName("");
                            renewData2.setIsRestor(0);
                        }
                        renewDataMapper.updateById(renewData2);
                    }
                }
            }
        }else{
            if(isNewStudent){
                if(residueCourseCount>=7){
                    if(renewData!=null){
                        renewData.setIsRenew(1);
                        if(inGrade!=null){
                            renewData.setSchoolId(inGrade.getSchoolId());
                            Staff staff = staffMapper.findOneById(inGrade.getTeacherId());
                            renewData.setTeacherId(inGrade.getTeacherId());
                            renewData.setTeacherName(staff.getName());
                        }else {
                            renewData.setSchoolId(inSchoolId);
                            renewData.setTeacherId(0L);
                            renewData.setTeacherName("");
                            renewData.setIsRestor(0);
                        }
                        renewDataMapper.updateById(renewData);
                    }
                    if(renewData2!=null){
                        if(inGrade!=null){
                            renewData2.setSchoolId(inGrade.getSchoolId());
                            Staff staff = staffMapper.findOneById(inGrade.getTeacherId());
                            renewData2.setTeacherId(inGrade.getTeacherId());
                            renewData2.setTeacherName(staff.getName());
                        }else {
                            renewData2.setSchoolId(inSchoolId);
                            renewData2.setTeacherId(0L);
                            renewData2.setTeacherName("");
                            renewData.setIsRestor(0);
                        }
                        renewDataMapper.updateById(renewData2);
                    }
                }
            }else {
                if(residueCourseCount>=7){
                    if(renewData!=null){
                        if(inGrade!=null){
                            renewData.setSchoolId(inGrade.getSchoolId());
                            Staff staff = staffMapper.findOneById(inGrade.getTeacherId());
                            renewData.setTeacherId(inGrade.getTeacherId());
                            renewData.setTeacherName(staff.getName());
                        }else {
                            renewData.setSchoolId(inSchoolId);
                            renewData.setTeacherId(0L);
                            renewData.setTeacherName("");
                            renewData.setIsRestor(0);
                        }
                        renewDataMapper.updateById(renewData);
                    }
                    if(renewData2!=null){
                        if(inGrade!=null){
                            renewData2.setSchoolId(inGrade.getSchoolId());
                            Staff staff = staffMapper.findOneById(inGrade.getTeacherId());
                            renewData2.setTeacherId(inGrade.getTeacherId());
                            renewData2.setTeacherName(staff.getName());
                        }else {
                            renewData2.setSchoolId(inSchoolId);
                            renewData2.setTeacherId(0L);
                            renewData2.setTeacherName("");
                            renewData2.setIsRestor(0);
                        }
                        renewDataMapper.updateById(renewData2);
                    }
                }
            }
            //添加报读的续班信息
            Integer year_part = iQuarterService.getQuarterYearPartForNum(inCourse.getQuarterNum());//获取报读的年份
            this.addData(inSchoolId,studentId,year_part,inCourse,inGrade);
        }



        return true;
    }


    public boolean deleteData(Long schoolId, Long studentId,Long subjectsId,Integer yearPart,Integer quarterNum){
        QueryWrapper<OrderDetail> orderDetailQueryWrapper = new QueryWrapper<>();
        orderDetailQueryWrapper.eq("school_id",schoolId);
        orderDetailQueryWrapper.eq("student_id",studentId);
        orderDetailQueryWrapper.eq("subjects_id",subjectsId);
        orderDetailQueryWrapper.eq("year_part",yearPart);
        orderDetailQueryWrapper.eq("quarter_num",quarterNum);
        orderDetailQueryWrapper.ne("status",2);
        orderDetailQueryWrapper.eq("deleted",0);

        Long count = orderDetailMapper.selectCount(orderDetailQueryWrapper);
        if(count==0){
            renewDataMapper.delete(new QueryWrapper<RenewData>().eq("school_id",schoolId).eq("subjects_id",subjectsId).eq("student_id",studentId).eq("year_part",yearPart).eq("quarter_num",quarterNum));
        }
        return true;
    }

    /**
     * 把学生从班级里删除，更新老师数据
     * @param
     * @param teacherId
     * @param studentId
     * @param subjectsId
     * @param yearPart
     * @param quarterNum
     * @return
     */
    public boolean deleteTeacherForGradeDelStudent(Grade grade, Long teacherId,Long studentId,Long subjectsId,Integer yearPart,Integer quarterNum){

        QueryWrapper<RenewData> renewDataQueryWrapper = new QueryWrapper<>();
        renewDataQueryWrapper.eq("school_id",grade.getSchoolId());
        renewDataQueryWrapper.eq("student_id",studentId);
        renewDataQueryWrapper.eq("subjects_id",subjectsId);
        renewDataQueryWrapper.eq("year_part",yearPart);
        renewDataQueryWrapper.eq("quarter_num",quarterNum);
        renewDataQueryWrapper.eq("teacher_id",teacherId);

         List<RenewData> renewDataList = renewDataMapper.selectList(renewDataQueryWrapper);

        if(renewDataList.size()>0){
            QueryWrapper<StudentGrade> studentGradeQueryWrapper = new QueryWrapper<>();
            studentGradeQueryWrapper.eq("school_id",grade.getSchoolId());
            studentGradeQueryWrapper.eq("subjects_id",subjectsId);
            studentGradeQueryWrapper.eq("year_part",yearPart);
            studentGradeQueryWrapper.eq("quarter_num",quarterNum);
            studentGradeQueryWrapper.eq("deleted",0);
            studentGradeQueryWrapper.ne("status",2);

            Long sgCount = studentGradeMapper.selectCount(studentGradeQueryWrapper);

            if(sgCount == 0){
                for (RenewData renewData:renewDataList)
                {
                    if(renewData.getTeacherId().equals(grade.getTeacherId())){
                        renewData.setTeacherId(0L);
                        renewData.setTeacherName("");
                        renewData.setIsRestor(0);
                        int rs = renewDataMapper.updateById(renewData);
                    }
                }
            }
        }
        return true;
    }



    /**
     * 获取有没有添加过报读的续班信息
     * @param schoolId
     * @param subjectsId
     * @param studentId
     * @param yearPart
     * @param quarterNum
     * @param type
     * @return
     */
    public RenewData getRenewDataForSubjects(Long schoolId,Long subjectsId,Long studentId,Integer yearPart,Integer quarterNum,Integer type){
        QueryWrapper<RenewData> renewDataQueryWrapper = new QueryWrapper<>();
        renewDataQueryWrapper.eq("school_id",schoolId);
        renewDataQueryWrapper.eq("subjects_id",subjectsId);
        renewDataQueryWrapper.eq("student_id",studentId);
        renewDataQueryWrapper.eq("year_part",yearPart);////////////////////
        renewDataQueryWrapper.eq("quarter_num",quarterNum);
        renewDataQueryWrapper.eq("type",type);
        renewDataQueryWrapper.last("limit 1");
        RenewData renewData = renewDataMapper.selectOne(renewDataQueryWrapper);
        return  renewData;
    }

    /**
     * 获取有没有续班信息
     * @param schoolId
     * @param subjectsId
     * @param studentId
     * @param yearPart
     * @param quarterNum
     * @param type
     * @return
     */
    public RenewData getRenewDataForRenew(Long schoolId,Long subjectsId,Long studentId,Integer yearPart,Integer quarterNum,Integer type){
        QueryWrapper<RenewData> renewDataQueryWrapper = new QueryWrapper<>();
        renewDataQueryWrapper.eq("school_id",schoolId);
        renewDataQueryWrapper.eq("subjects_id",subjectsId);
        renewDataQueryWrapper.eq("student_id",studentId);
        renewDataQueryWrapper.eq("year_part_renew",yearPart);//////
        renewDataQueryWrapper.eq("quarter_num_renew",quarterNum);
        renewDataQueryWrapper.eq("type",type);
        RenewData renewData = renewDataMapper.selectOne(renewDataQueryWrapper);
        return  renewData;
    }


    /**
     * 退费更新
     * @param schoolId
     * @param studentId
     * @param courseId
     * @param gradeId
     * @param orderDetailId
     * @return
     */
    public boolean refundUpdateData(Long schoolId, Long studentId, Long courseId, Long gradeId,Long orderDetailId){
        Course course = courseMapper.selectById(courseId);
        OrderDetail orderDetail = orderDetailMapper.selectById(orderDetailId);
        Grade gradeInfo = gradeMapper.selectById(gradeId);
        if(gradeInfo == null){
            return false;
        }

        //订单类型：1报名，2补费，3转课，4退费，5转校
        if(orderDetail.getOrderType().getKey() == 1){
            if(course!=null){
                QueryWrapper<StudentType> studentTypeQueryWrapper = new QueryWrapper<>();
                studentTypeQueryWrapper.eq("school_id",schoolId);
                studentTypeQueryWrapper.eq("student_id",studentId);
                studentTypeQueryWrapper.eq("subjects_id",course.getSubjectsId());
                StudentType studentTypeInfo = iStudentTypeService.getOne(studentTypeQueryWrapper);
                if(studentTypeInfo!=null){
                    Integer useCourseHouse = studentGradeMapper.useCourseCount(gradeId,studentId);

                    //1为新生，为报名
                    if(studentTypeInfo.getType().equals(1)){
                        if(useCourseHouse.equals(0)){
                            //这个方法是为了兼容老数据的，以后 删除掉
                            this.deleteTeacherData(schoolId,course.getSubjectsId(),studentId,gradeInfo.getYearPart());
                            this.deleteTeacherDataForOrderDetailId(orderDetailId);
                        }
                    }else {
                        //老生，为续费
                        if(useCourseHouse<=2){
                            //这个方法是为了兼容老数据的，以后 删除掉
                            this.deleteTeacherData(schoolId,course.getSubjectsId(),studentId,gradeInfo.getYearPart());
                            this.deleteTeacherDataForOrderDetailId(orderDetailId);
                        }
                    }
                }

            }

        }else if(orderDetail.getOrderType().getKey() == 3){

        }else if(orderDetail.getOrderType().getKey() == 5){

        }
        return true;
    }



    /**
     * 删除老师的续班信息
     * @param schoolId
     * @param subjectsId
     * @param studentId
     * @param yearPartRenew
     * @return
     */
    private boolean deleteTeacherData(Long schoolId,Long subjectsId,Long studentId,Integer yearPartRenew){
        QueryWrapper<RenewData> renewDataQueryWrapper  = new QueryWrapper<>();

        renewDataQueryWrapper.eq("school_id",schoolId);
        renewDataQueryWrapper.eq("subjects_id",subjectsId);
        renewDataQueryWrapper.eq("student_id",studentId);
        renewDataQueryWrapper.eq("year_part_renew",yearPartRenew);

        List<RenewData> renewDataList = renewDataMapper.selectList(renewDataQueryWrapper);
        for (RenewData renewData:renewDataList){
            UpdateWrapper<RenewData> updateWrapper = new UpdateWrapper<>();
            updateWrapper.eq("id",renewData.getId());
            updateWrapper.set("teacher_id",null);
            updateWrapper.set("teacher_name",null);
            renewDataMapper.update(null,updateWrapper);
        }
        return true;
    }

    /**
     * 删除老师的续班信息
     * @return
     */
    private boolean deleteTeacherDataForOrderDetailId(Long orderDetailId){
        QueryWrapper<RenewData> renewDataQueryWrapper  = new QueryWrapper<>();

        renewDataQueryWrapper.eq("order_detail_id",orderDetailId);

        List<RenewData> renewDataList = renewDataMapper.selectList(renewDataQueryWrapper);
        for (RenewData renewData:renewDataList){
            UpdateWrapper<RenewData> updateWrapper = new UpdateWrapper<>();
            updateWrapper.eq("id",renewData.getId());
            updateWrapper.set("teacher_id",null);
            updateWrapper.set("teacher_name",null);
            renewDataMapper.update(null,updateWrapper);
        }
        return true;
    }



    public IPage<ReNewDetailForTeacher> getRnewDetailForTeahcer(IPage<?> page, Wrapper wrapper){
        IPage<ReNewDetailForTeacher> reNewDetailForTeacherList = renewDataMapper.getStudentDetailList(page,wrapper);
        for (ReNewDetailForTeacher reNewDetailForTeacher:reNewDetailForTeacherList.getRecords()){
            Student student = studentMapper.selectById(reNewDetailForTeacher.getStudentId());
            if(student != null){
                reNewDetailForTeacher.setStudentName(student.getName());
            }else {
                //System.out.println("teacher_id:"+reNewDetailForTeacher.getStudentId());
            }

            if(reNewDetailForTeacher.getIsRenew().equals(1) && reNewDetailForTeacher.getTrueRenew().equals(1)){
                reNewDetailForTeacher.setIsRenew(1);
            }else {
                reNewDetailForTeacher.setIsRenew(0);
            }

            reNewDetailForTeacher.setTeachTeacherNameList(studentGradeMapper.getGradeTeacherNameList(reNewDetailForTeacher.getSchoolId(), reNewDetailForTeacher.getStudentId(),reNewDetailForTeacher.getSubjectsId(),reNewDetailForTeacher.getYearPart(),reNewDetailForTeacher.getQuarterNum()));

        }
        return reNewDetailForTeacherList;
    }
}
